package com.lanhai.zhonglai.listener;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.dispatch.DispatchHack;
import com.lanhai.zhonglai.utils.Tools;


@Component
public class HackListener {
	@Autowired
	DispatchHack hack;

	private boolean running;
	private Thread th;

	 private static final Logger logger =  LogManager.getLogger("agvlog");



	public void Start( ) {
		running=true;
		th=new Thread() {
			@Override
			public void run() {
				try{
					DatagramSocket	ds = new DatagramSocket(9000);
					while(running){
						byte data[] = new byte[20];
						DatagramPacket dp = new DatagramPacket(data, data.length);
						ds.receive(dp);

						if(data.length<5) continue;

						int id=Tools.toIntL(new byte[] {data[1],data[2],data[3],data[4]});
						byte type=data[0];
						if(type==1) {
							//释放交管
							logger.info("后台发送指令：释放交管：id="+id);
							hack.releaseControllerArea(id);
						}else if(type==2) {
							//重置交管区数据
							logger.info("后台发送指令：重置交管区数据");
							hack.reloadControllerArea();
						}else if(type==3) {
							//重置点位数据
							logger.info("后台发送指令：重置点位数据");
							hack.reloadDot( );
						}else if(type==4) {
							//清空agv状态
							logger.info("后台发送指令：清空agv状态:id="+id);
							hack.releaseAgv(id );

						}else if(type==5) {
							//释放充电桩
							logger.info("后台发送指令：释放充电桩:id="+id);
							hack.releasePower(id );
						}else if(type==6) {
							//启用充电桩
							logger.info("后台发送指令：启用充电桩:id="+id);
							hack.enablePower(id );
						}else if(type==7) {
							//禁用充电桩
							logger.info("后台发送指令：禁用充电桩:id="+id);
							hack.disablePower(id );
						}else if(type==8) {
							//启用机台
							logger.info("后台发送指令：启用机台：id="+id);
							hack.enableStation(id);
						}else if(type==9) {
							//禁用机台
							logger.info("后台发送指令：禁用机台：id="+id);
							hack.disableStation(id);
						}else if(type==10) {
							//释放机台
							logger.info("后台发送指令：释放机台：id="+id);
							hack.releaseStation(id);

						}else if(type==11) {
							//填充机台装料
							logger.info("后台发送指令：填充机台装料:id="+id);
							hack.fullStationMount(id );
						}else if(type==12) {
							//清空机台装料
							logger.info("后台发送指令：清空机台装料:id="+id);
							hack.emptyStationMount(id );
						}else if(type==13) {
							//重置机台数据
							logger.info("后台发送指令：重置机台数据:id="+id);
							hack.resetStation(id );
						}else if(type==14) {
							//反向缓存机进出
							logger.info("后台发送指令：反向缓存机进出:id="+id);
							hack.turnStation(id );
						}


					}
					ds.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}

		};
		th.start();
	}


	public void Stop() {
		running=false;
		try {
			th.interrupt();
			th=null;
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
